<?php


namespace app\general\controller;
use think\Controller;
use think\Db;
/**
 * 定时添加机器人
 */
class Robot extends Controller
{
    /**
     *  生成机器人
     */
    public function createRobot(){
        $images=Db::table("xys_sq_image")->field("image")->select();
        $len=count($images);
        //占20个
  /*      $city_one=array(
           "上海市","北京市","深圳市","广州市","郑州市","长沙市","沈阳市","青岛市","宁波市","东莞市",
           "成都市","杭州市","重庆市","武汉市","苏州市","西安市","天津市","南京市","无锡市","厦门市"
        );

        //45个
        $string="昆明市#大连市#荆州市#合肥市#佛山市#福州市#哈尔滨市#济南市#温州市#长春市#泉州市#南宁市#贵阳市#南昌市#南通市#金华市#徐州市#太原市#泰州市#济宁市#嘉兴市#烟台市#惠州市#保定市#台州市#中山市#绍兴市#乌鲁木齐市#潍坊市#兰州市#珠海市#镇江市#海口市#扬州市#临沂市#洛阳市#唐山市#呼和浩特市#盐城市#汕头市#银川市#淄博市#邯郸市#芜湖市#漳州市#绵阳市#桂林市#三亚市#遵义市#咸阳市#揭阳市#沧州市#商丘市#连云港市#柳州市#岳阳市#信阳市#株洲市#衡阳市#襄阳市#九江市#大庆市#许昌市#新乡市#宁德市#西宁市#宿迁市#菏泽市#蚌埠市#邢台市#滁州市#肇庆市#德阳市#曲靖市#秦皇岛市#潮州市#吉林市#常德市#宜春市#黄冈市#泸州市#驻马店市#宜昌市#湛江市#鞍山市#湘潭市#铜陵市#阜阳市#南阳市#威海市#上饶市#莆田市#廊坊市#包头市#赣州市#淮安市#石家庄市#常州市#湖州市#江门市";
        $city_two=explode("#",$string);
        //20个
        $string2="张家口市#张家界市#拉萨市#开封市#丽江市#贵港市#安庆市#娄底市#丹东市#玉林市#南平市#梅州市#汕尾市#三门峡市#梧州市#崇左市#吉安市#马鞍山市#防城港市#黄山市#景德镇市#三明市#龙岩市#鹰潭市#六安市#钦州市#北海市#枣庄市#云浮市#营口市#双鸭山市#铁岭市#韶关市#承德市#河源市#益阳市#铜仁市#商洛市#资阳市#延安市#乐山市#咸宁市#晋城市#怀化市#宣城市#清远市#舟山市#庆阳市#葫芦岛市#大同市#焦作市#盘锦市#广安市#汉中市#阳江市#平顶山市#茂名市#郴州市#佳木斯市#呼伦贝尔市#张掖市#六盘水市#玉溪市#酒泉市#周口市#牡丹江市#抚顺市#攀枝花市#临汾市#宝鸡市#荆门市#十堰市#广元市#普洱市#孝感市#黄石市#邵阳市#临沧市#宜宾市#雅安市";
        $city_there=explode("#",$string2);
        //15个
        $string3="石嘴山市#朔州市#赤峰市#巴彦淖尔市#乌兰察布市#鄂尔多斯市#襄樊市#榆林市#运城市#中卫市#阳泉市#通辽市#齐齐哈尔市#鹤岗市#玉林市#日喀则市#绥化市#乌海市#那曲市#吐鲁番市#黑河市#伊春市#渭南市#四平市#辽源市#白山市#通化市#内江市#七台河市#嘉峪关市#白城市#松原市#宿州市#朝阳市#本溪市#辽阳市#衢州市#三沙市#锦州市#昌都市#池州市#阜新市#山南市#百色市#金昌市#淮南市#鄂州市#萍乡市#丽水市#鸡西市#淮北市#亳州市#东营市#白银市#新余市#定西市#眉山市#聊城市#永州市#自贡市#德州市#哈密市#濮阳市#抚州市#遂宁市#武威市#海东市#漯河市#泰安市#巴中市#安阳市#固原市#随州市#晋中市#吕梁市#长治市#衡水市#吴忠市#克拉玛依市#铜川市#昭通市#滨州市#平凉市#保山市#毕节市#来宾市#天水市#日照市#河池市#安康市#忻州市#鹤壁市#儋州市#林芝市#南充市#达州市#陇南市#莱芜市#安顺市#贺州市";
        $city_four=explode("#",$string3);*/
        $string2="大理#楚雄#德宏#迪庆#阿拉善#锡林郭勒#兴安#大兴安岭#白沙#保亭#昌江#澄迈#阿克苏#阿拉尔#阿勒泰#巴音郭楞#果洛#海北#海南#海西#潜江#神农架#天门#仙桃#陵水#琼海#琼中#屯昌#克孜勒苏#石河子#塔城#图木舒克#红河#怒江#文山#西双版纳#延边#巢湖#济阳#恩施#定安#东方#乐东#临高#博尔塔拉#昌吉#和田#喀什#黄南#玉树#甘南#临夏#湘西#阿坝#甘孜#凉山#万宁#文昌#五指山#黔东南#五家渠#伊犁#黔南#黔西南#阿里";
        $city_there=explode("#",$string2);
        DB::startTrans();
        $formData=array();
        for($i=0;$i<20;$i++){
            $image_index=rand(0,$len-1);
            $city_index=rand(0,count($city_there)-1);
            $formData[]=array(
                "nick_name"=>$this->createNickName(),
                "avatar"=>$images[$image_index]['image'],
                "province"=>"中国",
                "city"=>$city_there[$city_index],
                "country"=>"",
                "is_robot"=>1
            );
        }
        //开始添加
   /*     for($i=0;$i<50;$i++){
            $image_index=rand(0,$len-1);
           // $city_index=rand(0,count($city_one)-1);
            $formData[]=array(
                "nick_name"=>$this->createNickName(),
                "avatar"=>$images[$image_index]['image'],
                "province"=>"",
               // "city"=>$city_one[$city_index],
                "city"=>'曲靖',
                "country"=>"中国",
                "is_robot"=>1
            );
        }*/
/*        for($i=0;$i<45;$i++){
            $image_index=rand(0,$len-1);
            $city_index=rand(0,count($city_two)-1);
            $formData[]=array(
                "nick_name"=>$this->createNickName(),
                "avatar"=>$images[$image_index]['image'],
                "province"=>"",
                "city"=>$city_two[$city_index],
                "country"=>"中国",
                "is_robot"=>1
            );
        }
        for($i=0;$i<20;$i++){
            $image_index=rand(0,$len-1);
            $city_index=rand(0,count($city_there)-1);
            $formData[]=array(
                "nick_name"=>$this->createNickName(),
                "avatar"=>$images[$image_index]['image'],
                "province"=>"中国",
                "city"=>$city_two[$city_index],
                "country"=>"",
                "is_robot"=>1
            );
        }
        for($i=0;$i<15;$i++){
            $image_index=rand(0,$len-1);
            $city_index=rand(0,count($city_four)-1);
            $formData[]=array(
                "nick_name"=>$this->createNickName(),
                "avatar"=>$images[$image_index]['image'],
                "province"=>"中国",
                "city"=>$city_two[$city_index],
                "country"=>"",
                "is_robot"=>1
            );
        }*/
        $id = $this->recordTimers(6);
       $flag=Db::table("xys_sq_user")->insertAll($formData);
        if($flag){
            Db::commit();
            $this->updateTimerType($id,"添加机器人成功");
            echo "添加机器人成功";
        }else{
            Db::rollback();
            $this->updateTimerType($id,"添加机器人失败");
            echo "添加机器人失败";
        }
    }
     /**
      * 生成昵称
      */
     public function createNickName(){
        $name=Db::table("xys_sq_nickname")->select();
        $index=rand(1,count($name));
        return $name[$index-1]['name'];
     }
     /**
      * 爬取图片
      */
     public function getImage(){
      /*   $title=array("男士发型","女士发型","乌龟","蚂蚁",
             "微信头像","2018男士短发","蜡笔小新","灰太狼","猫","狗",
             "鱼","象","刘亦菲","赵丽颖","赵天爱","罗志祥","篮球明星","篮球高清头像","动漫头像男",
             "动漫头像女","动漫头像女生可爱萌","动漫头像女生可爱萌 清新可爱","2019男士短发","山水画",
             "王子","qq头像");*/
         /*$title=array("卡通老虎","卡通猪","卡通男生","卡通女生","火头像","美女","模特","玫瑰花头像",
             "百合头像","跑酷头像","朱头像","李头像","王头像","吴头像","刘头像","孙头像","老王头像",
             "非主流头像","心头像","小孩头像","下雨头像","伤感头像","励志头像","快乐头型","小清新头像",
             "可爱头像","可爱女头像","可爱男头像");*/
     /*    $title=array("旅行头像","背包客头像","背包头像","女生背包头像","女生头像高冷","女生头像霸气",
             "抖音头像女生冷酷霸气","霸气头像女生超拽冷酷","风景头像","40岁女人花朵微信头像","吉祥的牡丹花微信头像",
             "微信头像吉祥","最美吉祥微信头像","大富大贵微信招财头像","男人最招财的微信头像","2019新版微信头像","2019最新微信动漫头像",
             "微信最吉利的好看头像","属蛇最吉利的微信头像");*/
        /* $title=array("健身头像","姓氏头像","姓氏头像男生","2018姓氏微信头像","2018微信卡通头像","呆萌头像",
             "呆萌头像女生","呆萌头像女生可爱","呆萌头像女生可爱 真人","呆萌头像女生可爱 真人 清纯","超萌女生头像小清新萌",
             "超萌头像","超萌头像女生动漫","风景头像 山水","风景头像 山水 花朵","花朵头像","花朵头像 淡雅","纹身头像男",
             "纹身头像","纹身头像女","后背纹身女","女生纹身","最适合女生的帅气纹身","纹身头像男 欧美","纹身头像男 社会人 霸气");*/
    /*     $title=array(
             "孤独头像","复古头像","文艺头像","森系头像","温暖头像","红唇头像","90后头像",
             "00后头像","海边头像","青春头像","字母头像","英伦风头像","嘟嘴头像","80后头像",
             "q版头像","超人头像","海绵宝宝头像","英雄联盟头像","小丸子头像","卡通动物头像",
             "卡卡西头像","火影头像","二次元头像","龙猫头像","美少女战士头像","死神头像","奥特曼头像",
             "进击的巨人头像","黑子的篮球头像","黑执事头像","夏目友人账头像","犬夜叉头像","黑子的篮球头像",
             "黑执事头像","炫舞头像","七龙珠头像","芭比娃娃头像","镜音双子头像","小破孩头像"
         );*/
        $title=array();
         echo count($title)*30;
         $formData=array();
         foreach ($title as $ti){
             $images=$this->get_thing_images_url_baidu($ti);
             $data=array();
             foreach ($images as $im){
                 $formData[]=array(
                     "image"=>$im,
                 );
             }
         }
         $id = $this->recordTimers(5);
         $flag=Db::table("xys_sq_image")->insertAll($formData);
         if($flag){
             return "爬取成功";
         }else{
             return "爬取失败";
         }
     }
    /**
     * 获取网页
     */
    function get_ssl_html($url) {
        //file_get_contents()
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_REFERER, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
    function get_thing_images_url_baidu($th) {//获取食材图片url
        $thing = urlencode($th);
        $url = "https://image.baidu.com/search/index?tn=baiduimage&word=$thing";
        $html = $this->get_ssl_html($url);
        $pattern = '/thumbURL":"(.+?)"/is';
        preg_match_all($pattern, $html, $match);
        $arr=array();
        foreach ($match[1] as $img){
            if (strstr($img,"jpg")) {
                if(strlen($img)<100){
                    $arr[]=$img;
                }

            }
        }
        return $arr;
    }

    /**
     * 生成汉字
     * @param $num
     * @return 汉字数量
     */
    function getChar($num)
    {
        $b = '';
        for ($i=0; $i<$num; $i++) {
            // 使用chr()函数拼接双字节汉字，前一个chr()为高位字节，后一个为低位字节
            $a = chr(mt_rand(0xB0,0xD0)).chr(mt_rand(0xA1, 0xF0));
            // 转码
            $b .= iconv('GB2312', 'UTF-8', $a);
        }
        return $b;
    }
    //记录定时器调用时间
    public function recordTimers($type)
    {
        $data = [
            'type' => $type,
            'create_time' => time(),
            'status' => 0
        ];
        return db('sq_record_timer')->insertGetId($data);
    }

    public function updateTimerType($id,$remark='')
    {
        db('sq_record_timer')->where('id', $id)->update(['status'=>1, 'remark'=>$remark]);
    }
}